package com.banshuibao.bsb_android.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.CommitAnswerModel;
import com.banshuibao.bsb_android.model.OneDetailsModel;
import com.banshuibao.bsb_android.model.OneModel;
import com.banshuibao.bsb_android.util.GetLoginInfo;
import com.banshuibao.bsb_android.util.ImageUtil;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.PriceUtil;
import com.banshuibao.bsb_android.util.TextPaintUtils;
import com.banshuibao.bsb_android.util.TimeUtil;
import com.banshuibao.bsb_android.util.XToast;

public class NoAnswerActivity extends BaseActivity implements View.OnClickListener {

    private TextView commitOne,commitTwo,commitThree;
    private ImageView head,heFristImg,heSecondImg;
    private TextView name,mineFristName,mineSecondName,mineThreeName,mineFristContent,mineSecondContent,mineThreeContent,mineFristDate,mineSecondDate,mineThreeDate;
    private TextView content,heFristName,heFristContent,heFristDate,heSecondName,heSecondContent,heSecondDate;
    private TextView date,money;
    private EditText mEditone,mEdittwo,mEditthree;
    private ImageView image_one;
    private ImageView image_two;
    private ImageView image_three;
    private String uuid;
    private String[] split;
    private RelativeLayout answer,retive2,retive3,retive4,retive5,retive6,retive7,retive8,retive9;
    private LinearLayout linearLayout;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_no_answer);
        initView();
        OneModel.DataBean item = (OneModel.DataBean) getIntent().getSerializableExtra("item");
        String uuid = item.getUuid();
        if (!TextUtils.isEmpty(uuid)) {
            setDetailsPost(uuid);
        }
    }

    private void setDetailsPost(String uuid) {
        OneDetailsModel.body body = new OneDetailsModel.body();
        body.setUuid(uuid);
        body.setIsWho("he");
        HttpUtil.doPost(Constants.Url.ONE_LIST_DETAILS, body, new HttpResponse(this,OneDetailsModel.class) {
            @Override
            public void onResult(Object obj) {
                OneDetailsModel s = (OneDetailsModel) obj;
                OneDetailsModel.DataBean data = s.getData();
                getData(data);
            }
        });
    }

    private void getData(OneDetailsModel.DataBean item) {
        if (item != null) {
            uuid = item.getUuid();
            String headImg = item.getHeadImg();
            String name1 = item.getName();
            double price = item.getPrice();
            String question = item.getQuestion();
            String imgs = item.getImgs();
            ImageUtil.loadImageHead(this, headImg, head);
            if (!TextUtils.isEmpty(name1)) {
                name.setText(name1);
                TextPaintUtils.textMothed(name);
            }
            content.setText(question);
            money.setText("¥"+ PriceUtil.FormatDecimal(price));
            String time = null;
            if (item.getCreateDate() != 0) {
                time = TimeUtil.format(item.getCreateDate());
            } else {
                time = "";
            }
            date.setText(time);
            if (!TextUtils.isEmpty(imgs)) {
                split = imgs.split(",");
                if (split.length > 0) {
                    if ("null".equals(split[0]))
                        return;
                    if (split.length == 1) {
                        ImageUtil.loadImage(this, split[0], image_one);
                        image_two.setVisibility(View.GONE);
                        image_three.setVisibility(View.GONE);
                    } else if (split.length == 2) {
                        ImageUtil.loadImage(this, split[0], image_one);
                        ImageUtil.loadImage(this, split[1], image_two);
                        image_three.setVisibility(View.GONE);
                    } else if (split.length == 3) {
                        ImageUtil.loadImage(this, split[0], image_one);
                        ImageUtil.loadImage(this, split[1], image_two);
                        ImageUtil.loadImage(this, split[2], image_three);
                    }
                }
            } else {
                image_one.setVisibility(View.GONE);
                image_two.setVisibility(View.GONE);
                image_three.setVisibility(View.GONE);
                linearLayout.setVisibility(View.GONE);
            }
            tv.setVisibility(View.VISIBLE);

            String status1 = item.getStatus();
            GetLoginInfo getLoginInfo = GetLoginInfo.getLoginInfoInstance(this).readUser();
            String name = getLoginInfo.getName();
            switch (status1) {
                //未回答
                case "0":
                    answer.setVisibility(View.VISIBLE);
                    retive2.setVisibility(View.VISIBLE);
                    commitOne.setVisibility(View.VISIBLE);
                    break;
                //已回答
                case "1":
                    answer.setVisibility(View.VISIBLE);
                    retive3.setVisibility(View.VISIBLE);
                    setDataOne(item,name);
                    break;
                //第一次追问
                case "2":
                    answer.setVisibility(View.VISIBLE);
                    retive3.setVisibility(View.VISIBLE);
                    retive4.setVisibility(View.VISIBLE);
                    retive5.setVisibility(View.VISIBLE);
                    commitTwo.setVisibility(View.VISIBLE);
                    setDataTwo(item,name);
                    break;
                //专家第一次回答追问
                case "3":
                    answer.setVisibility(View.VISIBLE);
                    retive3.setVisibility(View.VISIBLE);
                    retive4.setVisibility(View.VISIBLE);
                    retive6.setVisibility(View.VISIBLE);
                    setDataThree(item,name);
                    break;
                //第二次追问
                case "4":
                    answer.setVisibility(View.VISIBLE);
                    retive3.setVisibility(View.VISIBLE);
                    retive4.setVisibility(View.VISIBLE);
                    retive6.setVisibility(View.VISIBLE);
                    retive7.setVisibility(View.VISIBLE);
                    retive8.setVisibility(View.VISIBLE);
                    commitThree.setVisibility(View.VISIBLE);
                    setDataFour(item,name);
                    break;
                //专家回答第二次追问
                case "5":
                    answer.setVisibility(View.VISIBLE);
                    retive3.setVisibility(View.VISIBLE);
                    retive4.setVisibility(View.VISIBLE);
                    retive6.setVisibility(View.VISIBLE);
                    retive7.setVisibility(View.VISIBLE);
                    retive9.setVisibility(View.VISIBLE);
                    setDataFive(item,name);
                    break;
                //过期
                case "6":

                    break;
            }
        }
    }

    private void setDataOne(OneDetailsModel.DataBean item,String name) {
        //答案
        String answer1 = item.getAnswer();
        mineFristName.setText(name);
        TextPaintUtils.textMothed(mineFristName);
        mineFristContent.setText(answer1);
        String time = null;
        if (item.getAnsDate() != 0) {
            time = TimeUtil.format(item.getAnsDate());
        } else {
            time = "";
        }
        mineFristDate.setText(time);
    }

    private void setDataTwo(OneDetailsModel.DataBean item,String name) {
        setDataOne(item,name);
        String headImg = item.getHeadImg();
        String name1 = item.getName();
        if (!TextUtils.isEmpty(headImg)) {
            ImageUtil.loadImageHead(this, headImg, heFristImg);
        }
        if (!TextUtils.isEmpty(name1)) {
            heFristName.setText(name1);
        }
        heFristContent.setText(item.getFirstAsked());
        String time = null;
        if (item.getFirstAskTime() != 0) {
            time = TimeUtil.format(item.getFirstAskTime());
        } else {
            time = "";
        }
        heFristDate.setText(time);
    }

    private void setDataThree(OneDetailsModel.DataBean item,String name) {
        setDataTwo(item,name);
        //答案
        mineSecondName.setText(name);
        TextPaintUtils.textMothed(mineSecondName);
        mineSecondContent.setText(item.getFirstAnswer());
        String time = null;
        if (item.getFirstAskAnswerTime() != 0) {
            time = TimeUtil.format(item.getFirstAskAnswerTime());
        } else {
            time = "";
        }
        mineSecondDate.setText(time);
    }

    private void setDataFour(OneDetailsModel.DataBean item,String name) {
        setDataThree(item,name);
        String headImg = item.getHeadImg();
        String name1 = item.getName();
        if (!TextUtils.isEmpty(headImg)) {
            ImageUtil.loadImageHead(this, headImg, heSecondImg);
        }
        if (!TextUtils.isEmpty(name1)) {
            heSecondName.setText(name1);
        }
        heSecondContent.setText(item.getSecondAsked());
        String time = null;
        if (item.getSecondAskTime() != 0) {
            time = TimeUtil.format(item.getSecondAskTime());
        } else {
            time = "";
        }
        heSecondDate.setText(time);
    }

    private void setDataFive(OneDetailsModel.DataBean item,String name) {
        setDataFour(item,name);
        //答案
        mineThreeName.setText(name);
        TextPaintUtils.textMothed(mineThreeName);
        mineThreeContent.setText(item.getSecondAnswer());
        String time = null;
        if (item.getSecondAskAnswerTime() != 0) {
            time = TimeUtil.format(item.getSecondAskAnswerTime());
        } else {
            time = "";
        }
        mineThreeDate.setText(time);
    }

    private void initView() {
        tv = findViewById(R.id.tv);
        setTitleText("一对一咨询详情");
        //用户问题信息
        head = (ImageView) findViewById(R.id.mine_img);
        name = (TextView) findViewById(R.id.mine_name);
        content = (TextView) findViewById(R.id.question);
        date = (TextView) findViewById(R.id.mine_date);
        money = (TextView) findViewById(R.id.mine_money);

        //我第一次回答信息
        mineFristName = findViewById(R.id.frist_ask_name);
        mineFristContent = findViewById(R.id.frist_ask_content);
        mineFristDate = findViewById(R.id.frist_ask_date);

        //用户第一次追问信息
        heFristImg = findViewById(R.id.common_head);
        heFristName = findViewById(R.id.common_name);
        heFristContent = findViewById(R.id.common_content);
        heFristDate = findViewById(R.id.common_date);

        //我第一次回答追问
        mineSecondName = findViewById(R.id.second_ask_name);
        mineSecondContent = findViewById(R.id.second_ask_content);
        mineSecondDate = findViewById(R.id.second_ask_date);

        //用户第二次追问信息
        heSecondImg = findViewById(R.id.common_head_two);
        heSecondName = findViewById(R.id.common_name_two);
        heSecondContent = findViewById(R.id.common_content_two);
        heSecondDate = findViewById(R.id.common_date_two);

        //我第二次回答追问
        mineThreeName = findViewById(R.id.third_ask_name);
        mineThreeContent = findViewById(R.id.third_ask_content);
        mineThreeDate = findViewById(R.id.third_ask_date);

        //三个发布回答按钮
        commitOne = findViewById(R.id.commit_one);
        commitTwo = findViewById(R.id.commit_two);
        commitThree = findViewById(R.id.commit_three);

        //答案横条
        answer = findViewById(R.id.answer);
        //第一次输入答案框
        retive2 = findViewById(R.id.relative_two);
        //第一次显示答案信息
        retive3 = findViewById(R.id.relative_three);
        //第一次显示追问信息
        retive4 = findViewById(R.id.relative_four);
        //第二次输入答案框
        retive5 = findViewById(R.id.relative_five);
        //第二次显示答案信息
        retive6 = findViewById(R.id.relative_six);
        //第二次显示追问信息
        retive7 = findViewById(R.id.relative_seven);
        //第三次输入答案框
        retive8 = findViewById(R.id.relative_eight);
        //第三次显示答案信息
        retive9 = findViewById(R.id.relative_nine);

        mEditone = (EditText) findViewById(R.id.edit);
        mEdittwo = (EditText) findViewById(R.id.edit_two);
        mEditthree = (EditText) findViewById(R.id.edit_three);
        image_one = (ImageView) findViewById(R.id.image_one);
        image_two = (ImageView) findViewById(R.id.image_two);
        image_three = (ImageView) findViewById(R.id.image_three);
        linearLayout = (LinearLayout) findViewById(R.id.liner);
        image_one.setOnClickListener(this);
        image_two.setOnClickListener(this);
        image_three.setOnClickListener(this);
        commitOne.setOnClickListener(this);
        commitTwo.setOnClickListener(this);
        commitThree.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.commit_one:
                String s1 = mEditone.getText().toString().trim();
                if (!TextUtils.isEmpty(s1)) {
                    showProgressDialog();
                    setPost(s1);
                } else {
                    XToast.showToast(this, "请输入答案之后在提交");
                }
                break;
            case R.id.commit_two:
                String s2 = mEdittwo.getText().toString().trim();
                if (!TextUtils.isEmpty(s2)) {
                    showProgressDialog();
                    setPostTwo(s2);
                } else {
                    XToast.showToast(this, "请输入答案之后在提交");
                }
                break;
            case R.id.commit_three:
                String s3 = mEditthree.getText().toString().trim();
                if (!TextUtils.isEmpty(s3)) {
                    showProgressDialog();
                    setPostTwo(s3);
                } else {
                    XToast.showToast(this, "请输入答案之后在提交");
                }
                break;
            case R.id.image_one:
                IntentManager.goBitmapShowActivity(NoAnswerActivity.this, split, 0);
                break;
            case R.id.image_two:
                IntentManager.goBitmapShowActivity(NoAnswerActivity.this, split, 1);
                break;
            case R.id.image_three:
                IntentManager.goBitmapShowActivity(NoAnswerActivity.this, split, 2);
                break;
        }
    }

    private void setPost(String s) {
        CommitAnswerModel model = new CommitAnswerModel();
        model.setContent(s);
        if (!TextUtils.isEmpty(uuid)) {
            model.setUuid(uuid);
        }
        HttpUtil.doPost(Constants.Url.ONE_ANSWER, model, new HttpResponse(this) {
            @Override
            public void onResult(Object obj) {
//                String s1 = (String) obj;
//                LogUtil.e(s1);
                XToast.showToast(NoAnswerActivity.this, "提交成功，感谢您的回答");
                finish();
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                cancelProgressDialog();
            }
        });
    }

    private void setPostTwo(String s) {
        CommitAnswerModel model = new CommitAnswerModel();
        model.setContent(s);
        if (!TextUtils.isEmpty(uuid)) {
            model.setUuid(uuid);
        }
        HttpUtil.doPost(Constants.Url.ANSWER_ONE_ASK, model, new HttpResponse(this) {
            @Override
            public void onResult(Object obj) {
//                String s1 = (String) obj;
//                LogUtil.e(s1);
                XToast.showToast(NoAnswerActivity.this, "提交成功，感谢您的回答");
                finish();
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                cancelProgressDialog();
            }
        });
    }
}
